package com.example.demo.controller;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.example.demo.entity.Race;
import com.example.demo.mapper.RaceLogMapper;
import com.example.demo.mapper.RaceMapper;
import com.example.demo.quant.DataProvider;
import com.example.demo.quant.QuantEngine;
import com.example.demo.quant.QuantRace;
import com.example.demo.quant.Strategy;
import com.example.demo.quant.impl.RandomDataProvider;
import com.example.demo.quant.impl.RandomStrategy;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RestController;

import java.time.LocalDateTime;
import java.util.List;

@Slf4j
@RestController
@RequiredArgsConstructor
public class RaceController {
    private final RaceMapper raceMapper;

    private final RaceLogMapper raceLogMapper;

    @PostMapping("quant/race-run")
    public void run(String raceName, Integer raceRound) {
        Race race = new Race()
                .setName(raceName)
                .setDescription("描述, raceRound=" + raceRound)
                .setCreatedAt(LocalDateTime.now());

        raceMapper.insert(race);

        DataProvider dataProvider = new RandomDataProvider(); // 提供数据
        Strategy strategy = new RandomStrategy(); // 策略

        QuantRace quantRace = new QuantRace();
        quantRace.setBalance(10000); // 账户余额
        quantRace.setPeriod(1000); // 每隔多久判断一次
        quantRace.setRound(raceRound); // 跑多少轮

        quantRace.setRaceId(race.getId());

        quantRace.setDataProvider(dataProvider);
        quantRace.setStrategy(strategy);

        QuantEngine engine = new QuantEngine();
        engine.setRaceMapper(this.raceMapper);
        engine.setRaceLogMapper(this.raceLogMapper);
        engine.runRace(quantRace);
    }

    @PostMapping("quant/race-list")
    public List<Race> list() {
        List<Race> raceList = raceMapper.selectList(Wrappers.lambdaQuery());
        log.warn("#17 raceList=" + raceList);
        return raceList;
    }

    @PostMapping("quant/race-by-id")
    public Race byId(Long id) {
        return raceMapper.selectById(id);
    }
}
